import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import menuList from '@/assets/menu'
import { generateRoutesFromMenu } from '@/common/utils'
//引入nprogress
import NProgress from 'nprogress'
import 'nprogress/nprogress.css' //这个样式必须引入
import auth from "@/common/auth"
import store from '@/store'
import {mapGetters, mapActions, mapMutations} from 'vuex'
// 简单配置

NProgress.inc(0.2)
NProgress.configure({ easing: 'ease', speed: 500, showSpinner: false })

Vue.use(Router)

const routes = generateRoutesFromMenu(menuList)
console.log(routes)
const router = new Router({
  mode: 'history',
  routes: routes
})
let b=true
router.beforeEach((to,from,next) => {
  if(to.path === '/pcAddress'){
    next()
    return
  }
  //console.log(to)
  NProgress.start()//加载进度条

  if(!auth.loggedIn() && to.path !== '/login'){
    next({
      path: '/login',
      query: {redirect: to.fullPath}
    })
  }else{// 已经登陆，意味着 有token
    if(b){
      b = false
      next({
        path: '/selDepartment'
      })
    }
    // 检查是否刷新加载
    if(to.path !== '/login'&&to.path !=='/'){
      store.dispatch('changeCurrentMenu',to)// 获取当前菜单,login页面没必要
    }
    next()
  }
})
router.afterEach(() => {
  NProgress.done()
})

export default router
